﻿using System;
using Firefly.ExtensionMethods;
using Firefly.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Firefly.Framework.Windows.Forms
{
    public class MessageBox : Form
    {
        private static MessageBox _messageBox;
        private Label _messageLabel;
        private Button _okButton;
        private Rectangle _clientArea;
        private Vector2 _messageSize;

        public MessageBox(string caption, string message)
        {
            Initialize(caption);
            InitializeSize(message);
            InitializeMessageLabel(message);
            InitializeOkButton();
            ResumeLayout();
            WindowsService.Instance.Desktop.Children.Add(this);
            Focus();
        }

        public static void Show(string caption, string message)
        {
            _messageBox = new MessageBox(caption, message);
        }

        private void OnOkClicked(object sender, Firefly.Framework.Input.MouseButtonMapEventArgs e)
        {
            WindowsService.Instance.Desktop.Children.Remove(this);
        }

        private void Initialize(string caption)
        {
            Parent = WindowsService.Instance.Desktop;
            ClientAreaOffset = WindowsService.Instance.Theme.Form.ClientAreaOffset;
            TextOffset = WindowsService.Instance.Theme.Form.TextOffset;
            DragHandle = new Rectangle(0, 0, 25, 25);
            DragHandleDock = DockStyle.Top;
            Draggable = true;
            Sizeable = false;
            Text = caption;
        }

        private void InitializeSize(string message)
        {
            _messageSize = WindowsService.Instance.Theme.Fonts[Font].MeasureString(FontSize, message);
            Rectangle viewPort = GraphicsService.Instance.Viewport.ToRectangle();
            int width = (int)(Padding.X + Padding.Z + ClientAreaOffset.X + _messageSize.X + ClientAreaOffset.X + ClientAreaOffset.Z);
            int height = (int)(Padding.Y + Padding.W + ClientAreaOffset.Y + _messageSize.Y + ClientAreaOffset.Y + ClientAreaOffset.W) + 5;
            Position = new Rectangle(viewPort.Width / 2,
                viewPort.Height / 2,
                width,
                height);
            _clientArea = new Rectangle((int)(DrawRectangle.X + Padding.X + ClientAreaOffset.X),
                (int)(DrawRectangle.Y + Padding.Y + ClientAreaOffset.Y),
                (int)(DrawRectangle.Width - Padding.X - Padding.Z - ClientAreaOffset.X - ClientAreaOffset.Z),
                (int)(DrawRectangle.Height - Padding.Y - Padding.W - ClientAreaOffset.Y - ClientAreaOffset.W));
            LayoutSuspended = false;
            Resize();
        }

        private void InitializeMessageLabel(string message)
        {
            _messageLabel = new Label();
            _messageLabel.Position = new Rectangle(0, 0, _clientArea.Width, (int)_messageSize.Y);
            _messageLabel.Text = message;
            _messageLabel.Parent = this;
            Children.Add(_messageLabel);
        }

        private void InitializeOkButton()
        {
            _okButton = new Button();
            _okButton.Text = "Ok";
            _okButton.Position = new Rectangle((_clientArea.Width / 2) - 25,
                (int)(_messageLabel.Position.Y + _messageLabel.Position.Height + 5),
                50,
                30);
            _okButton.Parent = this;
            _okButton.Clicked += new EventHandler<Firefly.Framework.Input.MouseButtonMapEventArgs>(OnOkClicked);
            Children.Add(_okButton);
        }
    }
}
